package com.zhidisoft.system.action;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zhidisoft.base.action.BaseAction;
import com.zhidisoft.common.Page;
import com.zhidisoft.common.ResultData;
import com.zhidisoft.system.entity.Role;
import com.zhidisoft.system.vo.RoleVO;

@SuppressWarnings("all")
@Controller
@Scope("prototype")
public class RoleAction extends BaseAction{
	
	private String userId;
	
	private RoleVO roleVO;//id=""
	
	private String ids;
	
	/**
	 * 直接转发至权限分配页面assign_role.jsp
	 * @return
	 */
	public String assign(){
		return "assign";
	}
	
	/**
	 * 直接转发至list_role.jsp
	 * @return
	 */
	public String list(){
		return "list";
	}
	
	
	/**
	 * 直接转发至edit_role.jsp
	 * @return
	 */
	public String edit(){
		
		return "edit";
	}
	
	/**
	 * 为指定的角色更新权限
	 * @return
	 */
	public String updateFunctions() throws Exception{
		boolean b = roleService.updateFunctions(roleVO.getId(), ids);
		if(b){
			resultData = ResultData.buildSuccessResult("为角色分配权限成功！");
		}else{
			resultData = ResultData.buildFailureResult("为角色分配权限失败！");
		}
		printJSONObject(resultData);
		return NONE;
	}
	
	/**
	 * 更新或者保存角色
	 * @return
	 * @throws IOException
	 */
	public String saveOrUpdate() throws IOException{
		//将vo转换为po
		Role role = RoleVO.toRole(roleVO);
		//判断当前是否为更新操作
		if( StringUtils.isNotBlank(role.getId()) ){
			role.setUpdateTime(new Date());
			role.setUpdateUser(this.getCurrentUser());
		}else{
			
			role.setCreateTime(new Date());
			role.setCreateUser(this.getCurrentUser());
		}
		
		//调用RoleServiceImpl中的saveOrUpdate()
		boolean b = roleService.saveOrUpdate(role);
		
		if(b){
			resultData = ResultData.buildSuccessResult("保存角色信息成功!");
		}else{
			resultData = ResultData.buildFailureResult("保存角色信息失败！");
		}
		printJSONObject(resultData);
		
		return NONE;
	}
	
	
	/**
	 * 分页获取角色信息
	 * @return
	 * @throws Exception
	 */
	public String findByPage() throws Exception{
		
		Page<RoleVO> pageBean = roleService.findByPage(page,rows);
		resultData = ResultData.buildSuccessResult("查询角色信息成功", pageBean);
		printJSONObject(resultData);
		return NONE;
	}
	
	/**
	 * 根据用户id获取该用户对应的角色信息
	 * @return
	 */
	public String findByUserId() throws Exception{
		List<Role> list = roleService.findRolesByUserId(userId);
		resultData = ResultData.buildSuccessResult("获取所有角色信息成功", list);
		printJSONObject(resultData, new String[]{"createUser","updateUser","functions","users"});
		return NONE;
	}
	
	/**
	 * 获取所有的角色
	 * @return
	 * @throws Exception
	 */
	public String findAll() throws Exception{
		
		List<Role> list = roleService.findAll();
		resultData = ResultData.buildSuccessResult("获取所有角色信息成功", list);
		printJSONObject(resultData, new String[]{"createUser","updateUser","functions","users"});
		
		return NONE;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public RoleVO getRoleVO() {
		return roleVO;
	}

	public void setRoleVO(RoleVO roleVO) {
		this.roleVO = roleVO;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	
}
